/**
 * Created by LENOVO on 2016/10/9.
 */
$(function () {
   all();
    
})
function edit(id) {
    $("#top").hide();
    $("#editpage").show();
    var id =id;
    var oldtitle = $("#title"+id).text();
    var olddesc= $("#description"+id).text();
    $("#title").val(oldtitle);
    $("#description").val(olddesc);
    var editpage = $("#editpage");
    var top = $("#top");
    var data = {};
    var url = "/Taxonomy/edit.press";
    data.id=id;
    $("#yes").click(function () {
        data.title = $("#title").val();
        data.desc =  $("#description").val();
        $.post(url,data,function (result) {
             if(result.msg=="true"){
                 alert("修改成功");
                 all();
             }else {
                 alert("修改失败");
                 editpage.show();
                 top.hide();
             }
        },"json");
    });
}
function dele(id) {
    var data = {};
    var url = "/Taxonomy/delete.press";
    data.taxonomy_id = id;
    $.post(url,data,function (result) {
        
    },"json");
}
function all() {
    $("#editpage").hide();
    $("#top").show();
    $.ajaxSetup({
        async:false
    });
    var url = "/Taxonomy/getall.press";
    data = {};
    $.post(url,data,function (result) {
        var newrow = "";
        for(var i = 0;i < result.list.length;i++){
            var id = result.list[i].taxonomy_id;
            var title = (result.list[i].taxonomy_title==null)?"":result.list[i].taxonomy_title;
            var description = (result.list[i].taxonomy_description==null)?"":result.list[i].taxonomy_description;
            var num = (result.num[id]==null)?"":result.num[id];
            var date = (result.list[i].taxonomy_created==null)?"":result.list[i].taxonomy_created;
            newrow += "<tr>"+
                "<td id='title"+id+"'>"+title+"</td><td id='description"+id+"'>"+description+"</td><td>"+num+"</td><td>"+date+"</td>"+
                " <td><button class='btn btn-xs btn-primary' onclick='edit("+id+")'> " +
                "<i class='glyphicon glyphicon-edit'></i></button>"+
                "<button class='btn btn-xs btn-danger' onclick='dele("+id+")'> <i class='glyphicon glyphicon-remove'></i></button>"+
                "</td></tr>";
            $("#table_demo2 tbody").html(newrow);
        }
    },"json");

    $('#table_demo2').find('.glyphicon-remove').parent('button').click(function(){
        if(confirm('确定要删除？')){
            $(this).closest('tr').remove();
        }
    });
}